/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *      pub_business.cpp
 * 位 置:
 *      src/biz/pub_business.cpp
 * 描 述:
 *		系统业务规则类
 * 备 注:
 *      无
 * 修 改:
 *      2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include "occi.h"
#include "pub/db.h"
#include "pub/str.h"
#include "pub/log.h"
#include "biz/pub_business.h"

using namespace std;

//----------------------------------------------
//	获取业务代码
//----------------------------------------------
String PubBusiness::getBusinessCode(String& serviceCode)
{
	return serviceCode.substr(3);
}

//----------------------------------------------
//  检查身份证号码是否合法
//----------------------------------------------
void PubBusiness::checkIdCard(String& idcard)
{
	char verCode[] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
	int weight[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

	Log::debug("检查身份证号码合法性...");

	if (idcard.length() != 15 && idcard.length() != 18)
	{
		Log::throwError("身份证号码必须为15位或18位.");
	}

	const char* pattern = "^([0-9]{6})(18|19|20)?([0-9]{2})([01][0-9])([0123][0-9])([0-9]{3})([0-9]|X)?$";
	if (!idcard.match(pattern))
	{
		Log::throwError("身份证号码不符合规则.");
	}

	if (idcard.length() == 18)
	{
		int sigma = 0;
		for (int i = 0; i < 17; i++)
		{
			sigma += (idcard[i] - '0') * weight[i];
		}

		if (verCode[sigma % 11] != idcard[17])
		{
			Log::throwError("18身份证校验位不合法！");
		}
	}

	Log::debug("身份证号码合法.");
}
